A good answer might be:

No. Integer and floating point types use different bit patterns to represent values.

Converting a String to a Double

The scheme used to represent integers is completely different from the scheme used to represent floating point. Even though 221 and 221.0 might be regarded as equivalent, the bit patterns that represent each one are completely different.

Floating point input using java.io is similar to integer input. With floating point, as with integers, you first read in characters. Then, if the characters are correctly formatted, they are converted to a numeric type.

Here is a program that converts a String of characters into primitive type double. In this program, the characters to be converted are contained in a String literal. (Later on we will read in characters from the keyboard.) The program will not run under versions of Java earlier than 1.2 because they do not have the parseDouble() method.

// This program requires Java 1.2 or higher
//
import java.io.*;
class StringToDouble
{
  public static void main (String[] args)
  {
    final String charData = "3.14159265";
    double value;

    value  = Double.parseDouble( charData  ) ;
    System.out.println("value: " + value +" twice value: " + 2*value );
  }
}

The reserved word final means that the value inside the variable charData is not allowed to change. Running the program writes the following to the screen:

C:\temp>java StringToDouble

value: 3.14159265 twice value: 6.2831853

It would be worth your effort to copy this program to a file and to compile and run it. The details of how this program works are on the next page.

QUESTION 2:

Would the program work if the declaration of charData were changed to:

final String charData = 3.14159265;